package edu.ptit.jad.data;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.ptit.jad.configuration.CONSTANTS;
import edu.ptit.jad.exception.AdException;
import edu.ptit.jad.model.Ad;
import edu.ptit.jad.model.AdContent;
import edu.ptit.jad.model.Document;

/**
 * 
 * @author PhuongDo Access files
 */
public class DataAccess {

	private static DataAccess dataAccess = null;

	public DataAccess() {

	}

	public static DataAccess getInstance() {

		if (dataAccess == null) {
			dataAccess = new DataAccess();
		}

		return dataAccess;
	}

	/**
	 * 
	 * @return List of stopwords from files
	 * @throws AdException
	 */

	public List<String> getStopWords() throws AdException {

		List<String> stopwords = new ArrayList<String>();

		File file = new File(CONSTANTS.FILE_STOPWORDS);
		String line = "";
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(file), "UTF8"));

			while ((line = reader.readLine()) != null) {

				if (line.equals(""))
					continue;
				stopwords.add(line.trim());

			}
		} catch (Exception e) {
			throw new AdException("read file " + CONSTANTS.FILE_STOPWORDS
					+ "  error!");
		}

		return stopwords;

	}

	/**
	 * 
	 * @return List of getSynonymWords from files
	 * @throws AdException
	 */

	public List<String> getSynonymWords() throws AdException {

		List<String> synonyWords = new ArrayList<String>();

		File file = new File(CONSTANTS.FILE_SYNONYMS);
		String line = "";
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(file), "UTF8"));

			while ((line = reader.readLine()) != null) {

				if (line.equals(""))
					continue;
				synonyWords.add(line.trim());

			}
		} catch (Exception e) {
			throw new AdException("read file " + CONSTANTS.FILE_SYNONYMS
					+ "  error!");
		}

		return synonyWords;

	}

	/**
	 * Get list documents form a file
	 * 
	 * @param fileDir
	 * @return
	 * @throws AdException
	 */

	public List<Document> getDocuments(String fileDir) throws AdException {

		List<Document> docs = new ArrayList<Document>();

		File file = new File(fileDir);
		String line = "";
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(file), "UTF8"));

			while ((line = reader.readLine()) != null) {

				if (line.equals(""))
					continue;
				docs.add(new Document(line.trim()));

			}
		} catch (Exception e) {
			throw new AdException("read file " + fileDir + "  error!");
		}

		return docs;

	}

	public List<Ad> getAds(String fileDir) throws AdException {

		List<Ad> docs = new ArrayList<Ad>();

		File file = new File(fileDir);
		String line = "";
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(file), "UTF8"));

			while ((line = reader.readLine()) != null) {

				if (line.equals(""))
					continue;
				docs.add(new Ad(line.trim()));

			}
		} catch (Exception e) {
			throw new AdException("read file " + fileDir + "  error!");
		}

		return docs;

	}

	public List<AdContent> getAdContents(String fileDir) throws AdException {

		List<AdContent> docs = new ArrayList<AdContent>();

		File file = new File(fileDir);
		String line = "";
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(file), "UTF8"));

			while ((line = reader.readLine()) != null) {

				if (line.equals(""))
					continue;

				// System.out.println(line);
				docs.add(new AdContent(line.trim()));

			}
		} catch (Exception e) {
			throw new AdException("read file " + fileDir + "  error!");
		}

		return docs;
	}

	public void saveAds(List<Ad> ads, String fileDir) throws Exception {

		File file = new File(fileDir);
		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(file, true)));

		for (Ad d : ads) {

			out.write(d.toString());
			out.write("\n");
			System.out.println(d.getId() + " done!");

		}
		out.close();

	}

	public HashMap<String, Integer> getCatesHashMap() {

		HashMap<String, Integer> hash = new HashMap<String, Integer>();

		File file = new File(CONSTANTS.FILE_DATA_CATES);
		String line = "";
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(file), "UTF8"));

			int index = 1;
			while ((line = reader.readLine()) != null) {

				if (line.equals(""))
					continue;
				// String[] rows = line.split("::");

				// System.err.println(line);
				hash.put(line.trim(), index);

				index++;

			}
		} catch (Exception e) {
			// throw new AdException("read file category  error!");
		}

		return hash;

	}

	/**
	 * 
	 * @return map of vocabularies
	 */

	public Map<String, Integer> getVocabularies() {

		Map<String, Integer> hash = new HashMap<String, Integer>();

		File file = new File(CONSTANTS.FILE_VOBS);
		String line = "";
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(file), "UTF8"));
			int index = 1;

			while ((line = reader.readLine()) != null) {

				if (line.equals(""))
					continue;

				hash.put(line, index);

				index++;
			}
		} catch (Exception e) {
			// throw new AdException("read file category  error!");
		}

		return hash;

	}

	public List<String> getContents(String fileDir) throws AdException {

		List<String> docs = new ArrayList<String>();

		File file = new File(fileDir);
		String line = "";
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(file), "UTF8"));

			while ((line = reader.readLine()) != null) {

				if (line.equals(""))
					continue;
				docs.add(line.trim());

			}
		} catch (Exception e) {
			throw new AdException("read file " + fileDir + "  error!");
		}

		return docs;

	}

	public static void main(String[] args) throws Exception {

		// List<Document> docs = DataAccess.getInstance().getDocuments(
		// CONSTANTS.FILE_DATA_NEWS);
		// for (Document d : docs) {
		// System.out.println(d);
		//
		// }

		List<Ad> ads = DataAccess.getInstance().getAds(CONSTANTS.FILE_DATA_ADS);
		DataAccess.getInstance().saveAds(ads, "data/trains/ads_full.jad");

	}

}
